﻿@model ZDSoft.TES.Entity.SystemEntity.Organization
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <style type="text/css">
.ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
	</style>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery-1.4.4.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/zTree/css/zTreeStyle/zTreeStyle.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.core-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.excheck-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.exedit-3.5.js")' type="text/javascript"></script>
    <script type="text/javascript">
       
        var setting = {
            view: {
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom,
                selectedMulti: false
            },
            edit: {
                enable: true,
                editNameSelectAll: true,
                showRemoveBtn: showRemoveBtn,
                showRenameBtn: showRenameBtn
            },
            data: {

                simpleData: {
                  enable: true

                }
            },
            callback: {
                beforeDrag: beforeDrag,
                beforeRemove: beforeRemove,
                beforeRename: beforeRename,
                onRename: onRename,
                  onRemove: onRemove
            }
        };
          
        function beforeDrag(treeId, treeNodes) {
            return false;
        };

        var zNodes = [
         @Html.Raw(TempData["type"].ToString())
		];

        function beforeRemove(treeId, treeNode) {
            var pNode = treeNode.getParentNode();
            if (pNode == null) {
                if (confirm('该节点为父节点，执行操作将删除所有相关节点，确定要删除吗？')) {
                    return true;
                }
                else { return false; }
            }
            var zTree = $.fn.zTree.getZTreeObj("treeOrganization");
            zTree.selectNode(treeNode);
        };
        var ss = "0";
        var k = true;
        function onRename(e, treeId, treeNode) {
            var parentNode = treeNode.getParentNode();
            var parentId = "";
            if (parentNode != null) {
                parentId = parentNode.id;
            }
            if (k == false) {
                ss = treeNode.id;
            }
            $.post("/Organization/Index", { "id": ss, "pid": parentId, "name": treeNode.name },
            function (data) {
                treeNode.id = data;
                ss = data;
                var zTree = $.fn.zTree.getZTreeObj("treeOrganization");
                zTree.updateNode(treeNode);
            });
            k = false;

        }

        function onRemove(e, treeId, treeNode)
        {
          $.post("/Organization/GetOrganizationByID", { "id":treeNode.id},
            function (data) {
                treeNode.id = data;
                ss = data;
                var zTree = $.fn.zTree.getZTreeObj("treeOrganization");
                zTree.updateNode(treeNode);
            });
        }
        function beforeRename(treeId, treeNode, newName) {
            if (newName.length == 0) {
                alert("节点名称不能为空.");
                var zTree = $.fn.zTree.getZTreeObj("treeOrganization");
                setTimeout(function () { zTree.editName(treeNode) }, 10);
                return false;
            }
           
            return true;
        };

        function showRemoveBtn(treeId, treeNode) {
            return true;
        };
        function showRenameBtn(treeId, treeNode) {
            return true;
        };

        var newCount = 1;
        function addHoverDom(treeId, treeNode) {
                var sObj = $("#" + treeNode.tId + "_span");
                if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
                var addStr = "<span class='button add' id='addBtn_" + treeNode.id
				+ "' title='add node' onfocus='this.blur();'></span>";
                sObj.append(addStr);
                var btn = $("#addBtn_" + treeNode.id);
                if (btn) btn.bind("click", function () {
                    var zTree = $.fn.zTree.getZTreeObj("treeOrganization");
                    zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.id, name: "new node" + (newCount++),icon: "../../Scripts/zTree/css/zTreeStyle/img/diy/3.png" });
                    return false;
                });
        };

        function removeHoverDom(treeId, treeNode) {

            $("#addBtn_" + treeNode.id).unbind().remove();

        };
        function add(e) {
            var zTree = $.fn.zTree.getZTreeObj("treeOrganization"),
			isParent = e.data.isParent,
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			if (treeNode) {
				treeNode = zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, isParent:isParent, name:"new node" + (newCount++)});
			} else {
				treeNode = zTree.addNodes(null, {id:(100 + newCount), pId:0, isParent:isParent, name:"new node" + (newCount++)});
			}
			if (treeNode) {
				zTree.editName(treeNode[0]);
			} else {
				alert("叶子节点被锁定，无法增加子节点");
			}
		};

        


        $(document).ready(function () {
         
         $.fn.zTree.init($("#treeOrganization"), setting,zNodes);
            $("#addParent").bind("click", { isParent: true }, add);

        });
    </script>
</head>
<body>
    <br />
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <div>
            <div>
                <span>1-@ZDSoft.TES.Entity.SystemEntity.OrganizationType.系部</span> <span>2-@ZDSoft.TES.Entity.SystemEntity.OrganizationType.教研室</span>
                <span>3-@ZDSoft.TES.Entity.SystemEntity.OrganizationType.班级</span>
            </div>
            <fieldset style="overflow: scroll; width: 100%; height: 100%">
                <legend style="color: #4169E1;">组织机构</legend>
                <div>
                    [ <a id="addParent" href="#" title="增加系部" onclick="return false;">添加节点</a> ]&nbsp;&nbsp;&nbsp;
                </div>
                <ul id="treeOrganization" class="ztree">
                </ul>
            </fieldset>
        </div>
       
       
    }
</body>
</html>
